//
//  SNTrackViewController.m
//  project
//
//  Created by 黄建华 on 2017/11/20.
//  Copyright © 2017年 黄建华. All rights reserved.
//

#import "SNTrackViewController.h"
#import "SNTrackCell.h"
#import "SNMapViewController.h"
#import "SNShareViewController.h"
#import "SDCycleScrollView.h"
@interface SNTrackViewController ()<UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate>
@property(nonatomic, strong) UITableView       *tableView;
@property(nonatomic, strong) NSMutableArray    *dataSource;
@property(nonatomic, assign) CGFloat           cellH;
@property(nonatomic, strong) UIButton          *btn;
@property(nonatomic, strong) SDCycleScrollView *scrollView;
@property (nonatomic, retain) UIPageControl    *pageControl;
@property (nonatomic, retain) NSMutableArray   *imageViewArr;
@property (nonatomic, assign) CGFloat          imageH;
@property (nonatomic, strong) UIView           *coverView;
@property (nonatomic, strong) UIImageView      *animateView;
@property (nonatomic, strong) UIView           *bgView;
@end

@implementation SNTrackViewController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    if ([FileModelEntity sharedInstance].otacanInfo) {
        NSLog(@"🙂直接弹OTA 11111");
        [self otaUpdateInfoView];
    } else {
        NSLog(@"🙂等待直接弹OTA 22222");
    }

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(OTACANINFONOTICE2) name:OTACANINFONOTICE object:nil];



    NSArray *array = [TrackModel getUserInfoFromDBWithUserID:KK_User.user_id];
    array             = [[array reverseObjectEnumerator] allObjects];
    self.dataSource   = array;
    _coverView.hidden = self.dataSource.count == 0 ? NO : YES;
    [_tableView reloadData];
//    [SN_Net getTrackDataWithSuccess:^(id object) {
//        self.dataSource = object;
//        _coverView.hidden = self.dataSource.count==0?NO:YES;
//        [_tableView reloadData];
//    } fail:^(id object) {
//
//    }];

    if (!IOSSHOWTARBARACTIVE) {
        return;
    }
    NSInteger width = 0;
    if (KK_ShareData.lastVcTag > KK_ShareData.currentVCTag) {
        width = HHHWIDTH;
    }
    if (KK_ShareData.lastVcTag < KK_ShareData.currentVCTag) {
        width = -HHHWIDTH;
    }

    if (KK_ShareData.lastVcTag == KK_ShareData.currentVCTag) {
        width = 0;
    }
    if (width != 0) {
        if (_bgView) {
            [_bgView removeFromSuperview];
            _bgView = nil;
        }
        if (_animateView) {
            [_animateView removeFromSuperview];
            _animateView = nil;
        }

        _animateView = [[UIImageView alloc]initWithFrame:self.view.frame];
        [self.view addSubview:_animateView];
        UIGraphicsBeginImageContextWithOptions(CGSizeMake(HHHWIDTH, HHHHEIGHT), NO, 0.0);
        [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        _animateView.image = viewImage;

        _bgView                 = [[UIView alloc] initWithFrame:self.view.frame];
        _bgView.backgroundColor = self.view.backgroundColor;
        [self.view addSubview:_bgView];
        [self.view bringSubviewToFront:_animateView];
        _animateView.frame  = SNFRAME(width, 0, HHHWIDTH, HHHHEIGHT);
        _animateView.hidden = NO;
        _bgView.hidden      = NO;
        [UIView animateWithDuration:VIEWANIMATETIME animations:^{
             _animateView.frame = SNFRAME(0, 0, HHHWIDTH, HHHHEIGHT);
         } completion:^(BOOL finished) {
             _animateView.hidden = YES;
             _bgView.hidden = YES;
         }];
    }
}

- (void)OTACANINFONOTICE2 {
    NSLog(@"🙂2222 otaUpdateInfoView");
    [self otaUpdateInfoView];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    [[NSNotificationCenter defaultCenter]removeObserver:self name:OTACANINFONOTICE object:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataSource = [NSMutableArray array];
    [self addTableView];
    
//    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}

- (void)addTableView {
//
//
//    UIImage *image = UIImageNamed(@"1-1");
//    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 20+FitSimpleIpx(0, 20), HHHWIDTH, image.size.height*HHHWIDTH/image.size.width)];
//
//    _imageH = backView.height;
//
////    _imageViewArr = [NSMutableArray arrayWithObjects:UIImageNamed(@"1-1"), UIImageNamed(@"2-1"), UIImageNamed(@"3-1"), nil];
////    _scrollView   = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, HHHWIDTH, _imageH) imageNamesGroup:_imageViewArr];
////    [backView addSubview:_scrollView];
////    [self.view addSubview:backView];
//
//    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, HHHWIDTH/6+6, HHHWIDTH/6+6)];
//    view.backgroundColor    = UIColorRGB(208, 207, 198);
//    view.center             = CGPointMake(HHHWIDTH*5/6, backView.bottom);
//    view.layer.cornerRadius = (HHHWIDTH/6+6)/2;
//    view.clipsToBounds      = YES;
//    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(2, 2, HHHWIDTH/6+2, HHHWIDTH/6+2)];
//    view1.backgroundColor    = UIColorRGB(144, 146, 141);
//    view1.layer.cornerRadius = (HHHWIDTH/6+2)/2;
//    view1.clipsToBounds      = YES;
//    [view addSubview:view1];
//    [self.view addSubview:view];
//    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
//    animation.duration            = 2.0;
//    animation.repeatCount         = HUGE_VALF;
//    animation.autoreverses        = YES;
//    animation.beginTime           = CACurrentMediaTime();
//    animation.timingFunction      = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
//    animation.removedOnCompletion = NO;
//    animation.fillMode            = kCAFillModeForwards;
//    animation.fromValue           = [NSNumber numberWithFloat:1.0];
//    animation.toValue             = [NSNumber numberWithFloat:1.1];
//    [view.layer addAnimation:animation forKey:@"scale-layer"];
//
//    _btn        = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, HHHWIDTH/6, HHHWIDTH/6)];
//    _btn.center = CGPointMake(HHHWIDTH*5/6, backView.bottom);
//    [_btn setTitleNormal:@"Go"];
//    [_btn setTitleColorNormal:[UIColor blackColor]];
//    _btn.backgroundColor    = [UIColor whiteColor];
//    _btn.layer.cornerRadius = HHHWIDTH/12;
//    _btn.clipsToBounds      = YES;
//    [_btn addTouchUpTarget:self action:@selector(pushMapVC)];
//    [self.view addSubview:_btn];
    


    self.stateView.backgroundColor = [UIColor clearColor];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:SNFRAME(0, 0, screenWidth, 215 +FitSimpleIpx(0, 44))];
    imageView.image = [UIImage imageNamed:@"BANNER"];
    
    _tableView                              = [[UITableView alloc]initWithFrame:CGRectMake(0,-FitSimpleIpx(20, 44), HHHWIDTH,screenHeight-17) style:UITableViewStylePlain];
    _tableView.delegate                     = self;
    _tableView.dataSource                   = self;
    _tableView.backgroundColor              = [UIColor clearColor];
    _tableView.separatorStyle               = UITableViewCellSeparatorStyleNone;
    _tableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_tableView];
    
    _tableView.tableHeaderView = imageView;
    
    self.view.backgroundColor = UIColorHEX(0xF2F4F5);
    
    UIButton *btn = [[UIButton alloc] initWithFrame:SNFRAME(screenWidth/2 - 40, screenHeight - 129-40, 80, 80)];
    [btn setImage:[UIImage imageNamed:@"GO222"] forState:UIControlStateNormal];
    [self.view addSubview:btn];
    [btn addTouchUpTarget:self action:@selector(pushMapVC)];

//    _coverView        = [[UIView alloc]initWithFrame:CGRectMake(0, _imageH+HHHWIDTH/8, HHHWIDTH, HHHHEIGHT-49-20-FitSimpleIpx(0, 20)-_imageH-HHHWIDTH/8)];
//    _coverView.hidden = self.dataSource.count == 0 ? NO : YES;
//    [self.view addSubview:_coverView];
//    [self.view bringSubviewToFront:_btn];
//    UIImage     *image2     = UIImageNamed(@"空白集中文");
//    UIImageView *coverImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, FitH(40), HHHWIDTH, image2.size.height*HHHWIDTH/image2.size.width)];
//    [coverImage setImage:SN_USER.lanuage == 0 ? UIImageNamed(@"空白集中文") : UIImageNamed(@"空白集英文")];
//    [_coverView addSubview:coverImage];
}

- (void)pushMapVC {
    SNMapViewController *mapVC  = [[SNMapViewController alloc]init];
    CustomTabBar        *tabBar = (CustomTabBar *)self.tabBarController;

    [tabBar hiddenTheTabBarView];
    [self.navigationController pushViewController:mapVC animated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.cellH;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SNTrackCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    if (!cell) {
        cell                = [[SNTrackCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    self.cellH = cell.cellH;
    TrackModel *model = self.dataSource[indexPath.row];
    [cell setInfoWithModel:model];
    
    if (_dataSource.count == 0) {
        
    }
    if (_dataSource.count -1 == indexPath.row) {
        cell.line.hidden = YES;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    SNShareViewController *shareVC = [[SNShareViewController alloc]init];

    shareVC.model = self.dataSource[indexPath.row];
    CustomTabBar *tabBar = (CustomTabBar *)self.tabBarController;
    [tabBar hiddenTheTabBarView];
    [self.navigationController pushViewController:shareVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
